/*******************************************************************************
 * Copyright (c) 2006, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Qi Liang (IBM Corporation)
 *******************************************************************************/
package net.jonbuck.tassoo.chart.ui.chart;

import net.jonbuck.tassoo.chart.ui.data.IDataSet;

import org.eclipse.birt.chart.model.ChartWithoutAxes;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.attribute.impl.FontDefinitionImpl;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.TextDataSet;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TextDataSetImpl;
import org.eclipse.birt.chart.model.impl.ChartWithoutAxesImpl;
import org.eclipse.birt.chart.model.layout.Plot;
import org.eclipse.birt.chart.model.type.PieSeries;
import org.eclipse.birt.chart.model.type.impl.PieSeriesImpl;

/**
 * Builds pie chart.
 * 
 * @since 1.0.0
 */
public class PieChartBuilder extends AbstractChartBuilder {

	SeriesDefinition sdX = null;

	/**
	 * Constructor.
	 * 
	 * @param dataSet
	 *            data for chart
	 */
	public PieChartBuilder(IDataSet dataSet) {
		super(dataSet);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ibm.examples.chart.widget.chart.AbstractChartBuilder#createChart()
	 */
	protected void createChart() {
		chart = ChartWithoutAxesImpl.create();
		chart.setSeriesThickness(25);
		chart.getBlock().setBackground(ColorDefinitionImpl.WHITE());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibm.examples.chart.widget.AbstractChartBuilder#buildPlot()
	 */
	protected void buildPlot() {
		Plot p = chart.getPlot();
		p.getClientArea().setBackground(null);
		p.getClientArea().getOutline().setVisible(false);
		p.getOutline().setVisible(false);
	}

	/**
	 * 
	 */
	protected void buildLegend() {
		chart.getLegend().setVisible(true);
		chart.getLegend().setPosition(Position.OUTSIDE_LITERAL);
		chart.getLegend().setShowPercent(true);
		chart.getLegend().getText().setFont(new FontDefinitionImpl("Arial", 10));
	}

	/**
	 * 
	 */
	protected void buildXSeries() {
		Object obj = dataSet.getXseries();
		TextDataSet categoryValues = TextDataSetImpl.create(obj);

		Series seCategory = SeriesImpl.create();
		seCategory.setDataSet(categoryValues);

		// Apply the color palette
		sdX = SeriesDefinitionImpl.create();
		sdX.getSeriesPalette().update(ColorDefinitionImpl.BLUE());

		if (((ChartWithoutAxes) chart).getSeriesDefinitions().size() > 0) {
			((ChartWithoutAxes) chart).getSeriesDefinitions().clear();
			((ChartWithoutAxes) chart).getSeriesDefinitions().add(sdX);
			sdX.getSeries().clear();
			sdX.getSeries().add(seCategory);
		} else {
			((ChartWithoutAxes) chart).getSeriesDefinitions().add(sdX);
			sdX.getSeries().add(seCategory);
		}
	}

	/**
	 * 
	 */
	protected void buildYSeries() {

		Object obj = dataSet.getYSeries();
		NumberDataSet orthoValuesDataSet = NumberDataSetImpl.create(obj);

		PieSeries sePie = (PieSeries) PieSeriesImpl.create();
		sePie.setDataSet(orthoValuesDataSet);

		SeriesDefinition sdY = SeriesDefinitionImpl.create();

		sdX.getSeriesDefinitions().add(sdY);
		sdY.getSeries().add(sePie);
	}

}
